package server

import (
	"context"
	"errors"
	"user-srv/basic/globar"
	__ "user-srv/basic/proto"
	"user-srv/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {

	return &__.RegisterResp{}, nil
}

//视频添加
func (s *Server) Videoadd(_ context.Context, in *__.VideoaddReq) (*__.VideoaddResp, error) {
	var v model.Video
	v.Status = int(in.Status)
	v.Image = in.Image
	//res := until.Chect(in.Title)
	//if res == false {
	//	return nil, errors.New("触发敏感词")
	//}
	v.Title = in.Title
	v.Types = int(in.Types)
	v.Num = int(in.Num)
	v.UserId = int(in.UserId)
	v.Info = in.Info
	v.VideoID = int(in.VideId)
	globar.DB.Where("video = ?", in.VideId).Find(&v)
	if in.VideId == 1 {
		return nil, errors.New("已关联其他视频")
	}
	if err := v.GetOneFile(globar.DB, in.Title); err != nil {
		return nil, errors.New("获取数据失败")
	}
	if err := v.Create(globar.DB); err != nil {
		return nil, errors.New("添加失败")
	}
	return &__.VideoaddResp{
		Id: int64(v.ID),
	}, nil
}

//视频修改
func (s *Server) VideoUpdate(_ context.Context, in *__.VideoUpdateReq) (*__.VideoUpdateResp, error) {
	var v model.Video
	v.Status = int(in.Status)
	v.Info = in.Info
	globar.DB.Find(&v)
	if err := v.GetUpdate(globar.DB, int(v.ID)); err != nil {
		return nil, errors.New("修改失败")
	}
	return &__.VideoUpdateResp{
		Id: int64(v.ID),
	}, nil
}

//视频查询
func (s *Server) VideoList(_ context.Context, in *__.VideoListReq) (*__.VideoListResp, error) {
	var v model.Video
	globar.DB.Find(&v)
	if v.UserId == 0 {
		return nil, errors.New("您不是会员不能查看")
	}
	cover, err := v.GetCover(globar.DB)
	if err != nil {
		return nil, err
	}
	return &__.VideoListResp{
		List: cover,
	}, nil
}

//状态查看
func (s *Server) VideoStatus(_ context.Context, in *__.VideoStatusReq) (*__.VideoStatusResp, error) {
	var v model.Video
	globar.DB.Find(&v)
	if err := v.GetStatus(globar.DB, int(v.ID)); err != nil {
		return nil, errors.New("查看转态失败")
	}
	return &__.VideoStatusResp{
		Status: int64(v.Status),
	}, nil
}
